<?php

namespace App\Controller\Admin;

use App\Constants\ErrorCode;
use App\Controller\InsideService\TaskInside;
use App\Middleware\AdminMiddleware;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

#[Controller(prefix: '/admin/Task'), Middleware(AdminMiddleware::class)]
class TaskController extends BaseController {

    /**
     * 获取平台列表
     * @return array
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[GetMapping(path: 'index')]
    public function index(): array {
        $userDataArr = $this->uInfo();
        $limit = $this->request->query('size', config('apiadmin.admin_list_default'));
        $game_hash = $this->request->query('game_hash', '');
        $page = $this->request->query('page', 1);

        if (empty($game_hash)) {
            $game_hash = $userDataArr['user_data']['bind_games'];
        }
        return (new TaskInside())->setCode($userDataArr['connection_code'])->index(
            $limit, $game_hash, (int)$page
        );
    }

    /**
     * 添加任务
     * @return array
     */
    #[PostMapping(path: 'add')]
    public function add(): array {
        $userDataArr = $this->uInfo();

        return (new TaskInside())->setCode($userDataArr['connection_code'])->add($this->request->post());
    }

    /**
     * 删除任务
     * @return array
     */
    #[GetMapping(path: 'del')]
    public function del(): array {
        $id = $this->request->query('id');
        $userDataArr = $this->uInfo();

        return (new TaskInside())->setCode($userDataArr['connection_code'])->del($id);
    }

    /**
     * 获取订阅消息
     * @return array
     */
    #[GetMapping(path: 'getMessage')]
    public function getMessage(): array {
        $game_hash = $this->request->query('game_hash', '');
        if (!$game_hash) {
            return $this->buildFailed(ErrorCode::PARAM_INVALID, '请选选择游戏');
        }
        $userDataArr = $this->uInfo();

        return (new TaskInside())->setCode($userDataArr['connection_code'])->getMessage($game_hash);
    }
}